package com.ewo.controller.workOrder;

import com.ewo.entity.ResponseResult;
import com.ewo.entity.workOrder.ReceiptInformation;
import com.ewo.entity.workOrder.WorkOrderInfo;
import com.ewo.service.workOrder.ReceiptInformationService;
import com.ewo.service.workOrder.WorkOrderInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/order")
public class WorkOrderInfoController {

    @Resource
    WorkOrderInfoService workOrderInfoService;
    @Resource
    ReceiptInformationService receiptInformationService;

    //查找所有工单池信息
    @GetMapping("/listOrder")
    public ResponseResult findAllWorkOrderInfo(){
        List<WorkOrderInfo> workOrderInfoList = workOrderInfoService.findAllWorkOrderInfo();
        if (workOrderInfoList == null || workOrderInfoList.size()==0){
            return ResponseResult.fail();
        }
        return ResponseResult.ok(workOrderInfoList);
    }

    //查找工单池为未抢单的工单信息
    @GetMapping("/initOrder")
    public ResponseResult findInitWorkOrderInfo(){
        List<WorkOrderInfo> workOrderInfoList = workOrderInfoService.findInitWorkOrderInfo();
        if (workOrderInfoList == null || workOrderInfoList.size()==0){
            return ResponseResult.fail();
        }
        return ResponseResult.ok(workOrderInfoList);
    }

    //根据工单id查找工单信息
    @GetMapping("/findOrder/{orderId}")
    public ResponseResult findWorkOrderInfoById(@PathVariable("orderId")int orderId){
        //根据id获取当前工单信息
        WorkOrderInfo workOrderInfo = workOrderInfoService.findWorkOrderInfoByOrderId(orderId);
        if (Objects.isNull(workOrderInfo) ){
            return ResponseResult.fail();
        }
        return ResponseResult.ok(workOrderInfo);
    }

    //根据工单id查找已完成的工单信息
    @GetMapping("/finishOrder/{orderId}")
    public ResponseResult findFinishWorkOrderInfoById(@PathVariable("orderId")int orderId){
        //根据id获取当前工单信息
        WorkOrderInfo workOrderInfo = workOrderInfoService.findWorkOrderInfoByOrderId(orderId);
        //根据完成的工单id返回回单信息
        ReceiptInformation receiptInformation = receiptInformationService.findReceiptInformation(orderId);
        if (Objects.isNull(workOrderInfo) || Objects.isNull(receiptInformation)){
            return ResponseResult.fail();
        }
        List<Object> objectList = new ArrayList<>();
        objectList.add(workOrderInfo);
        objectList.add(receiptInformation);
        return ResponseResult.ok(objectList);
    }



    //根据当前员工id查找相关工单
    @GetMapping("/homeOrder/{empId}")
    public ResponseResult homeWorkOrderById(@PathVariable("empId") int empId){
        //根据当前员工id查找相关状态不为1的工单
        List<WorkOrderInfo> workOrderInfoList = workOrderInfoService.findWorkOrderInfoByEmpId(empId);
        //根据empId查找今日工单数
        int today = workOrderInfoService.findTodayOrder(empId);
        //根据empId查找本月工单数
        int month = workOrderInfoService.findMonthOrder(empId);
        List<Object> objectList = new ArrayList<>();
        objectList.add(workOrderInfoList);
        objectList.add(today);
        objectList.add(month);
        if (workOrderInfoList== null || workOrderInfoList.size()==0 ){
            return ResponseResult.fail();
        }
        return ResponseResult.ok(objectList);
    }

    //抢单,抢单成功,状态为2待服务
    @GetMapping("/contendOrder/{orderId}/{empId}")
    public ResponseResult contendWorkOrderById(@PathVariable("orderId")int orderId,@PathVariable("empId") int empId){
        //根据id获取当前工单信息
        //1.修改当前工单状态为待服务
        int work_status = 2;
        //根据工单id修改当前工单状态,并将工单分配给此员工
        int rows = workOrderInfoService.updateWorkOrderInfoById(orderId, work_status,empId);
        return rows==0?ResponseResult.fail():ResponseResult.ok(null);
    }

    //开始服务,状态为3服务中
    @GetMapping("/ordering/{orderId}/{empId}")
    public ResponseResult orderingWorkOrderById(@PathVariable("orderId")int orderId,@PathVariable("empId") int empId){
        //根据id获取当前工单信息
        //1.开始服务,状态为3服务中
        int work_status = 3;
        //根据工单id修改当前工单状态
        int rows = workOrderInfoService.updateWorkOrderInfoById(orderId, work_status,empId);
        return rows==0?ResponseResult.fail():ResponseResult.ok(null);
    }

    //填写回单
    @PostMapping("/receipt")
    public ResponseResult addReceiptInformation(@ModelAttribute("receiptInformation") ReceiptInformation receiptInformation){
        receiptInformation.setReceipt_time( LocalDateTime.now());
        //增加回单信息
        int rows = receiptInformationService.addReceiptInformation(receiptInformation);
        //更改此工单状态为4
        workOrderInfoService.updateWorkOrderInfoById(receiptInformation.getO_id(),4,receiptInformation.getE_id());
        return rows==0?ResponseResult.fail():ResponseResult.ok(null);
    }

    //删除工单

    //取消工单
    @GetMapping("cancelOrder/{orderId}")
    public ResponseResult cancelWorkOrderByOrderId(@PathVariable("orderId") int orderId){
        int rows = workOrderInfoService.updateWorkStatusByOrderId(orderId,1);
        return rows == 0 ? ResponseResult.fail():ResponseResult.ok(null);
    }


}
